﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Linq.Expressions;


namespace PhotoGallery.Infrastructure.Helpers
{
    public static class ListHelper
    {
        public static IList<SelectListItem> ToSelectList<T>(this IList<T> list, Func<T, String> funText, Func<T, String> funValue)
        {
            return ToSelectList<T>(list, funText, funValue, string.Empty);
        }

        public static IList<SelectListItem> ToSelectList<T>(this IList<T> list, Func<T, String> funText, Func<T, String> funValue, String defaultValue)
        {
            IList<SelectListItem> slist = new List<SelectListItem>();
            foreach (T item in list)
            {
                string value = funValue(item);
                slist.Add(new SelectListItem { Text = funText(item), Value = value, Selected = value.Equals(defaultValue, StringComparison.OrdinalIgnoreCase) });
            }

            return slist;
        }

        public static void ForEach<T>(this IList<T> list, Action<T> action)
        {
            if (action == null)
                return;

            if (list == null)
                throw new ArgumentNullException("list");

            foreach (T item in list)
                action(item);
        }

        public static HashSet<T> ToHashSet<T>(this IEnumerable<T> list)
        {

            HashSet<T> results = new HashSet<T>();
            foreach (var item in list)
            {
                results.Add(item);
            }

            return results;
        }

        public static List<T> ToList<T>(this T[] array)
        {
            if (array == null)
                throw new ArgumentNullException("array is null!!!");

            List<T> result = new List<T>();
            for (int i = 0; i < array.Length; i++)
            {
                result.Add(array[i]);
            }

            return result;
        }
    }
}
